package com.lzs.bootsafety.common.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.common.oss.Exception.OssException;
import com.lzs.bootsafety.common.util.oss.FileUtils;
import com.qiniu.util.StringUtils;
import org.springframework.util.DigestUtils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
/**
 *  @author  zhanshi Liu
 *  @date    2020/04/21
 *  @website www.jhmsona.cn
 *
 */
public class AliApi extends BaseApi {
    private String aliAccessKey;
    private String aliSecretKey;
    private String aliBucket;
    private String aliBucketDomain ;
    private String aliEndpoint;
    private String aliStoragePath;
    private OSS ossClient;
    AliApi() {
        super("阿里云");
    }
    public AliApi init(String aliEndpoint, String aliAccessKey, String aliSecretKey, String aliBucketDomain, String aliBucket,String aliStoragePath) {
        this.aliAccessKey = aliAccessKey;
        this.aliSecretKey = aliSecretKey;
        this.aliBucket = aliBucket;
        this.aliBucketDomain = aliBucketDomain;
        this.aliEndpoint = aliEndpoint;
        this.aliStoragePath = aliStoragePath;
        this.ossClient = new OSSClientBuilder().build(this.aliEndpoint, this.aliAccessKey,this.aliSecretKey);
        return this;
    }


    @Override
    public SysFile uploadFile(InputStream is, String fileUrl) {
        this.check();
        Date startTime = new Date();
        String key = FileUtils.generateTempFileName(fileUrl);
        this.createNewName(fileUrl);
        String uploadName = this.aliStoragePath+this.newFileName;
        try {
            InputStream uploadIs = FileUtils.clone(is);
            InputStream fileHashIs = FileUtils.clone(is);
            this.ossClient.putObject(this.aliBucket,uploadName,uploadIs);
            return new SysFile()
                    .setOriginalFileName(key)
                    .setSuffix(this.suffix)
                    .setUploadStartTime(startTime)
                    .setUploadEndTime(new Date())
                    .setFilePath(this.aliStoragePath+this.newFileName)
                    .setFileHash(DigestUtils.md5DigestAsHex(fileHashIs))
                    .setFullFilePath(this.aliBucketDomain+this.aliStoragePath+ this.newFileName);
        } catch (IOException e) {
            throw new OssException("[" + this.storageType + "]文件上传失败：" + e.getMessage());
        } finally {
            this.ossClient.shutdown();
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public boolean download(String fileName){
        check();
        OSSObject ossObject = ossClient.getObject(this.aliBucket, fileName);
        BufferedInputStream is = new BufferedInputStream(ossObject.getObjectContent());
        FileUtils.downloadFile(fileName,is,this.storageType);
        ossClient.shutdown();
        return true;
    }

    @Override
    public boolean delete(String filePath) {
        check();
        ossClient.deleteObject(aliBucket,filePath);
        ossClient.shutdown();
        return true;
    }

    @Override
    protected void check() {
        if(null ==ossClient){
            throw new OssException("[" + this.storageType + "]尚未配置阿里云，文件上传功能暂时不可用！");
        }
    }
}
